// pages/02admin/13sellingGoods/sellingGoods.js
var app = getApp()
var appData = app.globalData
Page({
   data: {
      goodsList: [],
      pageNo: 1,
   },

   onLoad: function (opt) {
      this.getGoodsList(1)
   },

   getGoodsList: function (pageNo) {
      var param = {
         uid: appData.shopInfo.shopInfo.userid,
         pageno: pageNo
      }
      var t = this
      app.getJsonData('api/space/product_list.php', param, function (res) {
         t.setData({
            goodsList: t.data.goodsList.concat(res.data)
         })
      })

      app.setNoticetGoodsList(function (res) {
         t.updateGoodsList(res)
      })
   },

   updateGoodsList: function (r) {
      var list = this.data.goodsList
      for (var i = 0; i < list.length; i++) {
         if (r.id == list[i].id) {
            list[i] = r
            break
         }
      }
      this.setData({ goodsList: list })
   },

   editGoods: function (e) {
      if (app.globalData.loginMode != 'boss') {
         app.toast('需要掌柜登录才能操作')
         return
      }

      let i = e.currentTarget.id
      var url_end = ''
      if (i != '') {
         let oneGoods = JSON.stringify(this.data.goodsList[i])
         url_end = '?goods=' + oneGoods
      }

      wx.navigateTo({
         url: '/pages/02admin/12goodsShelves/goodsShelves' + url_end
      })
   },

   delGoods: function (e) {
      if (app.globalData.loginMode != 'boss') {
         app.toast('需要掌柜登录才能操作')
         return
      }

      let i = e.currentTarget.id
      var list = this.data.goodsList
      var param = { id: list[i].id }
      var that = this
      app.getNetData('product/mg/delete', param, function (res) {
         list.splice(i, 1)
         that.setData({ goodsList: list })
      })
   },

   onReachBottom: function () {
      this.data.pageNo++
      this.getGoodsList(this.data.pageNo)
   },
})